<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    private static $front = null;

    public function _initSession() {
        Zend_Session::start();
        $ss = new Zend_Session_Namespace(APPLICATION_KEY);
		if(empty($ss->type)){
			$ss->type = 'Guest';
		}
    }

    protected function _initDb() {

        $dbOption = $this->getOption('resources');

        $dbOption = $dbOption['db'];

        // Setup database
        $db = Zend_Db::factory($dbOption['adapter'], $dbOption['params']);

        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");

        Zend_Registry::set('connectDB', $db);

        //Khi thiet lap che do nay model moi co the su dung duoc
        Zend_Db_Table::setDefaultAdapter($db);

        // Return it, so that it can be stored by the bootstrap
        return $db;
    }

    protected function _initLoadRoute() {
        //Get Instance Front Controller
        self::$front = Zend_Controller_Front::getInstance();

        //Route Detail Page
        $detail_news = new Zend_Controller_Router_Route_Regex('tin-tuc/(.+)-(\d+).html$', array('module' => 'default', 'controller' => 'detail', 'action' => 'news'), array(1 => "title", 2 => "id"), 'tin-tuc/%s-%d.html');
        $detail_cate = new Zend_Controller_Router_Route_Regex('(.+)/(.+)-(\d+).html$', array('module' => 'default', 'controller' => 'detail', 'action' => 'category'), array(1 => "cate_name", 2 => "title", 3 => "cat_id"), '%s/%s-%d.html');
        $detail_product = new Zend_Controller_Router_Route_Regex('product/(.+)-(\d+).html$', array('module' => 'default', 'controller' => 'detail', 'action' => 'product'), array(1 => "name", 2 => "id"), 'product/%s-%d.html');

        //Add route        
        $routers = self::$front->getRouter();
        $routers->addRoute('detail_news', $detail_news);
        $routers->addRoute('detail_cate', $detail_cate);
        $routers->addRoute('detail_pro', $detail_product);
    }
    
    protected  function _initVisited(){
       
    }
    
    protected function _initLoadPlugin(){
		$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Zendvn_System_Plugin());
	}

}